import router from "./router";
import store from "./store";
import { Message } from "element-ui";
import NProgress from "nprogress"; // progress bar
import "nprogress/nprogress.css"; // progress bar style
import { getToken } from "@/utils/auth"; // get token from cookie
import getPageTitle from "@/utils/get-page-title";

NProgress.configure({ showSpinner: false }); // NProgress Configuration

// const whiteList = ["/login"]; // no redirect whitelist
const whiteList = ['/404', '/login'] //白名单
// 导航守卫
router.beforeEach(async (to, form, next) => {
  if (getToken()) {
    // 登陆了
    if (to.path === 'login') {
      next('/')
    } else {
      if (!store.getters.username) {
        let { roles } = await store.dispatch('user/getUserProfileAction')
        // console.log(store.getters.username);
        // console.log(roles);
        let routessss = []
        routessss = await store.dispatch('permission/filterRoutes', roles.menus)
        router.addRoutes([
          ...routessss,
          { path: '*', redirect: '/404', hidden: true }
        ])
        next({ ...to, replace: true })
      }
      else {
        next()
      }

    }
  } else {
    // 未登陆
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})
router.afterEach(() => {
  // finish progress bar
  NProgress.done();
});
